package File;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Ap0stoL2
 * Date: 2023-08-05
 * Time: 22:56
 */
//对6的补充
public class Demo66 {

    //使用try with resourses 更好的解决
    public static void main(String[] args) throws IOException {

        try (Reader reader = new FileReader("d:/test.txt")) {

            //大文件多次读入的策略,只有读到-1才代表大文件读完,如果文件足够大每次都会填满buf
            while (true) {
                //往buf里写东西
                //这种东西java中不常见,c++中常用
                char buf[] = new char[1024];
                int n = reader.read(buf);
                //读到文件末尾
                if (n == -1) {
                    break;
                }

                for (int i = 0; i < n; i++) {
                    System.out.print(buf[i] + " ");
                }
                System.out.println();

                //字符数组改字符串,但是一定要设置读入的范围,不然String会把buf没填满的东西全部装入string
                String s1= new String(buf,0,n);
                System.out.println(s1);
            }

        }

    }
}
